package com.ychs.lawyerback01.controller;

import com.ychs.lawyerback01.annotation.RequirePermission;
import com.ychs.lawyerback01.common.R;
import com.ychs.lawyerback01.common.ResultCode;
import com.ychs.lawyerback01.dto.UserOptDTO;
import com.ychs.lawyerback01.dto.UserQueryDTO;
import com.ychs.lawyerback01.service.UserService;
import com.ychs.lawyerback01.vo.UserDetailVo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户控制器
 * @author xuehongjin
 * @version 1.0
 * @date 2025/8/7
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;



    /**
     * 条件查询用户列表
     */
    @GetMapping("/getUserByCondition")
    @RequirePermission("system:user:query")
    public R<?> getUsersByCondition(UserQueryDTO queryDTO) {
        Map<String, Object> result = userService.getUsersByCondition(queryDTO);
        return R.success(result);
    }

    /**
     * 查询用户详情
     */
    @GetMapping("/detail/{id}")
    @RequirePermission("system:user:query")
    public R<?> getUserDetail(@PathVariable Long id) {
        UserDetailVo detailVo = userService.getUserDetail(id);
        return R.success(detailVo);
    }

    /**
     * 新增用户
     */
    @PostMapping("/addUser")
    @RequirePermission("system:user:add")
    public R<?> addUser(@Validated @ModelAttribute UserOptDTO userOptDTO) {
        int rows = userService.addUser(userOptDTO);
        return rows > 0 ? R.success() : R.fail(ResultCode.ADD_FAILE);
    }

    /**
     * 修改用户
     */
    @PostMapping("/editUser")
    @RequirePermission("system:user:edit")
    public R<?> editUser(@Validated @ModelAttribute UserOptDTO userOptDTO) {
        int rows = userService.editUser(userOptDTO);
        return rows > 0 ? R.success() : R.fail(ResultCode.UPDATE_FAILE);
    }

    /**
     * 批量删除用*/
    @DeleteMapping("/deleteUser")
    @RequirePermission("system:user:delete")
    public R<?> deleteUser(@RequestBody List<Long> ids) {
        int rows = userService.deleteUser(ids);
        return rows > 0 ? R.success() : R.fail(ResultCode.DELETE_FAILE);
    }
}
